package com.example.springboot014blogadmin.controller;


import cn.hutool.core.bean.BeanUtil;
//import com.example.springboot014blogadmin.exception.IdNullException;
import com.example.springboot014blogadmin.formatter.IdType;
import com.example.springboot014blogadmin.model.dto.ArticleDTO;
import com.example.springboot014blogadmin.model.param.ArticleParam;
import com.example.springboot014blogadmin.model.vo.ArticleVO;
import com.example.springboot014blogadmin.po.ArticlePO;
import com.example.springboot014blogadmin.service.ArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @description: TODO
 * @author: uu
 * @data: 2023/7/9 下午 03:58
 * @version: v1.0
 */

@RequiredArgsConstructor
@Controller
public class ArticleController {
    private final ArticleService articleService;
    // 阅读最多的前 10 篇文章
    @GetMapping( value = {"/", "/article/hot"})
    public String showHotArticle(Model model){
        System.out.println("begin------------>");
        List<ArticlePO> articlePOList = articleService.queryTopArticle();
//转为 VO
        List<ArticleVO> articleVOList = BeanUtil.copyToList(articlePOList,
                ArticleVO.class);
//存储数据
        model.addAttribute("articleList", articleVOList);
//视图
        return "/blog/articleList";
    }
    //添加文章
    @PostMapping("/article/add")
    public String addArticle(@Validated(ArticleParam.AddArticle.class) ArticleParam param){
        ArticleDTO article = new ArticleDTO();
        article.setTitle(param.getTitle());
        article.setSummary(param.getSummary());
        article.setContent(param.getContent());
        boolean add = articleService.addArticle(article);
        return "redirect:/article/hot";
    }
    //查询文章
    @GetMapping("/article/get")
    public String queryById(Integer id, Model model){
        ArticleDTO articleDTO = articleService.queryByArticleId(id);
        ArticleVO articleVO = BeanUtil.copyProperties(articleDTO, ArticleVO.class);
        model.addAttribute("article",articleVO);
        return "/blog/editArticle";
    }
    //修改文章
    @PostMapping("/article/edit")
    public String modifyArticle(@Validated(ArticleParam.EditArticle.class)
                                ArticleParam param){
//        System.out.println(param);
        boolean edit = articleService.modifyArticle(param);
        return "redirect:/article/hot";
    }
    //删除文章
    @PostMapping("article/removeArticle")
    public String removeArticle(@RequestParam("ids") IdType idType){
        System.out.println("ids="+idType);
        if(idType.getIdList() == null){
//            throw new IdNullException("Id 为 null");
            throw new RuntimeException("id为空");
        }
        articleService.removeArticle(idType.getIdList());
        return "redirect:/article/hot";
    }
    //查询文章开始的 1000 个字
    @GetMapping("/article/detail/overview")
    @ResponseBody
    public String queryDetail(Integer id){
        String top20Content = articleService.queryTop20Detail(id);
        return top20Content;
    }
}
